﻿// <copyright file="TeamFoundationContext.cs" company="Soumya Chattopadhyay">
// Copyright (c) 2010 All Right Reserved
// </copyright>
// <disclaimer> This software is intented for educational purposes only and not for 
// commerical or prodution use. The software is provided "as is", without warranty 
// of any kind, express or implied, including but not limited to the warranties of 
// merchantability, fitness for a particular purpose and noninfringement. In no event 
// shall the author(s) or copyright holder(s) be liable for any claim, damages or other 
// liability, whether in an action of contract, tort or otherwise, arising from, out of 
// or in connection with the software or the use or other dealings in the software.
// </disclaimer>

namespace My.CodePlex.TFSExplorer.TeamFoundation
{
    using System;
    using System.Windows.Forms;
    using Microsoft.TeamFoundation.Client;
    using Microsoft.TeamFoundation.Server;
    using Microsoft.TeamFoundation.VersionControl.Client;
    using My.CodePlex.TFSExplorer.Controls;
    using My.CodePlex.TFSExplorer.Resources;

    /// <summary>
    /// The current team foundation server context
    /// </summary>
    public class TeamFoundationContext
    {
        /// <summary>
        /// Initializes a new instance of the TeamFoundationContext class
        /// </summary>
        /// <param name="tfsServer">The TFS server</param>
        public TeamFoundationContext(TeamFoundationServer tfsServer)
        {
            if (tfsServer != null)
            {
                TeamFoundationContext.TeamFoundationServer = tfsServer;
                TeamFoundationContext.VersionControlServer = (VersionControlServer)tfsServer.GetService(typeof(VersionControlServer));
                TeamFoundationContext.VersionControlServer.NonFatalError += this.VersionControl_NonFatalError;
                TeamFoundationContext.GroupSecurityService = (IGroupSecurityService)tfsServer.GetService(typeof(IGroupSecurityService));

                Workstation currentWorkstation = Workstation.Current;
                WorkspaceInfo[] allWorkspaces = currentWorkstation.GetAllLocalWorkspaceInfo();

                TeamProject[] projs = TeamFoundationContext.VersionControlServer.GetAllTeamProjects(false);
                TeamFoundationContext.CurrentProject = projs[0];

                TeamFoundationContext.Credentials = new UICredentialsProvider();

                // Set the current displayed user to the default username
                TeamFoundationContext.CurrentDisplayUser = TeamFoundationContext.VersionControlServer.AuthenticatedUser;

                // Select the Workspace and mapped Path
                MappedPathWindow win = new MappedPathWindow(allWorkspaces);
                Nullable<bool> dialogResult = win.ShowDialog();

                WorkspaceInfo workspaceInfo = null;
                if (dialogResult.HasValue && dialogResult.Value)
                {
                    workspaceInfo = currentWorkstation.GetLocalWorkspaceInfo(win.MappedPath);
                }
                else
                {
                    // Choose a default
                    workspaceInfo = currentWorkstation.GetLocalWorkspaceInfo(allWorkspaces[0].MappedPaths[0]);
                }

                if (workspaceInfo != null)
                {
                    TeamFoundationContext.Workspace = TeamFoundationContext.VersionControlServer.GetWorkspace(workspaceInfo);
                }
            }
        }

        #region Properties

        /// <summary>
        /// Gets the team foundation server
        /// </summary>
        public static TeamFoundationServer TeamFoundationServer { get; private set; }

        /// <summary>
        /// Gets the version control server
        /// </summary>
        public static VersionControlServer VersionControlServer { get; private set; }

        /// <summary>
        /// Gets the workspace information
        /// </summary>
        public static Workspace Workspace { get; private set; }

        /// <summary>
        /// Gets the group security service
        /// </summary>
        public static IGroupSecurityService GroupSecurityService { get; private set; }

        /// <summary>
        /// Gets or sets the current displayed user
        /// </summary>
        public static string CurrentDisplayUser { get; set; }

        /// <summary>
        /// Gets the current project
        /// </summary>
        public static TeamProject CurrentProject { get; private set; }

        /// <summary>
        /// Gets the user credentials
        /// </summary>
        public static ICredentialsProvider Credentials { get; private set; }

        #endregion

        /// <summary>
        /// If there is a nonfatal error - we'd like to see it
        /// </summary>
        /// <param name="sender">The sender object</param>
        /// <param name="e">The event args</param>
        private void VersionControl_NonFatalError(object sender, Microsoft.TeamFoundation.VersionControl.Client.ExceptionEventArgs e)
        {
            MessageBox.Show(e.ToString(), Resource.ProductNameLong, MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
    }
}